package com.estar.assistsurvy.adapter;

import java.util.ArrayList;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.Item;
import com.estar.assistsurvy.widget.BadgeView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePageGridviewAdapter extends BaseAdapter {
	private ArrayList<Item> items;
	private LayoutInflater inflater;
	private Context context;
	private int noticeSum;
	
	public HomePageGridviewAdapter(ArrayList<Item> items, Context context) {
		super();
		this.items = items;
		this.context = context;
		inflater=LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams") 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder ;
		if(convertView == null){
			convertView=inflater.inflate(R.layout.gridview_homepage, null);
			holder=new ViewHolder();
			holder.gridItem_img=(ImageView) convertView.findViewById(R.id.gridItem_img);
			holder.gridItem_txt=(TextView) convertView.findViewById(R.id.gridItem_txt);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		
		Item item=items.get(position);
		holder.gridItem_img.setImageResource(item.getImg());
		holder.gridItem_txt.setText(item.getText());
		if(position==4){
			if(noticeSum>0){
				gvItem(holder.gridItem_img);
			}
		}
		return convertView;
	}
	
	private class ViewHolder{
		ImageView gridItem_img;
		TextView gridItem_txt;
	}
	
	public void setGvItem(int noticeSum){
		this.noticeSum = noticeSum;
	}
	
	public void gvItem(View view){
    	BadgeView badge = new BadgeView(context, view);
    	badge.setText(noticeSum+"");
    	badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
    	badge.setTextColor(Color.WHITE);
    	badge.setBadgeBackgroundColor(Color.RED);
    	badge.setTextSize(12);
    	badge.toggle();
    }

}
